package com.hhkj.admin.student.service.extend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.hhkj.admin.student.domain.Student;
import com.hhkj.admin.student.domain.StudentCourseTrainCount;
import com.hhkj.admin.student.model.res.TrainDetailRes;
import com.hhkj.admin.student.service.base.TrainCountService;
import com.hhkj.admin.student.service.extend.TrainCountExtendService;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.service.base.TrainService;
import com.hhkj.api.train.common.cont.ApiTrainConst;
import com.ruoyi.common.constant.SysUserCodeConstants;
import com.hhkj.admin.student.common.cont.StudentConst;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学员培训统计ServiceExtend接口
 *
 * @author ruoyi
 * @since 2023-06-08
 */
@Service
public class TrainCountExtendServiceImpl implements TrainCountExtendService {
    @Autowired
    private TrainCountService trainCountService;
    @Autowired
    private TrainService trainService;

    /**
     * 补充查询条件
     *
     * @param trainCount 前台传参
     */
    private void applyCondition(StudentCourseTrainCount trainCount) {
        //可见学员范围
        if (!SysUser.isAdminTypeCode(SecurityUtils.getTypeCode())) {
            if (SysUserCodeConstants.UserCode.ENTERPRISE.getCode().equals(SecurityUtils.getTypeCode())) {
                //企业只看见已通过+已过期
                trainCount.setEntId(SecurityUtils.getUserId());
                List<String> statusList = Lists.newArrayList();
                statusList.add(StudentConst.AuditCode.STATUS_PASS.getCode());
                statusList.add(StudentConst.AuditCode.STATUS_OUT_TIME.getCode());
                trainCount.setStatusList(statusList);
            } else {
                //机构看见自己导入的
                trainCount.setInsId(SecurityUtils.getUserId());
            }
        }
    }

    /**
     * 查询符合条件的学员
     */
    @Override
    public List<Student> selectStudentList(StudentCourseTrainCount countParam) {
        //查询条件补充
        applyCondition(countParam);
        return trainCountService.selectStudentList(countParam);
    }

    /**
     * 查询培训详情
     *
     * @param trainCount 传参
     * @return 培训详情
     */
    @Override
    public List<TrainDetailRes> pageTrainDetail(StudentCourseTrainCount trainCount) {
        LambdaQueryWrapper<Train> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Train::getStudentId, trainCount.getStudentId())
                .eq(Train::getCourseId, trainCount.getCourseId())
                .eq(Train::getTypeCode, trainCount.getTypeCode())
                .select(Train.class, x -> !x.getColumn().equals("answer_list"))
                .orderByDesc(Train::getSubmitTime);

        List<Train> trainList = trainService.list(queryWrapper);
        //处理计算
        return dealTrainDetail(trainCount, trainList);
    }

    /**
     * 处理计算
     *
     * @param trainCount 查询列表
     * @param trainList  查询出的培训数据列表
     * @return 处理计算后的培训详情列表
     */
    private List<TrainDetailRes> dealTrainDetail(StudentCourseTrainCount trainCount, List<Train> trainList) {
        List<TrainDetailRes> trainDetailList = Lists.newArrayList();
        for (Train train : trainList) {
            TrainDetailRes trainDetail = new TrainDetailRes();
            trainDetail.setTrainTime(train.getSubmitTime());
            trainDetail.setTrainCostTime(DateUtils.getFormatDate(train.getDurationSecond()));
            if (ApiTrainConst.TypeCode.TEST.getCode().equals(trainCount.getTypeCode())) {
                //如果是测试，查得分
                trainDetail.setMark(train.getMark());
            } else {
                //如果是练习，查得分率  (对的题/做的题)
                String s = MathUtils.calculateThePercentage(train.getRightAmount(), train.getAnswerAmount());
                trainDetail.setMarkRate(s);
                //练习题数
                trainDetail.setTrainAnswerAmount(train.getAnswerAmount());
            }
            trainDetailList.add(trainDetail);
        }
        return trainDetailList;
    }
}
